package com.example.service.impl;

import com.example.config.UploadBean;
import com.example.entity.Page.MyPageHelper;
import com.example.entity.Page.PageRequest;
import com.example.entity.SlideshowImage;
import com.example.mapper.SlideshowImageMapper;
import com.example.result.CommonResult;
import com.example.service.SlideshowImageService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author: tobed
 * @date: 2022/11/16
 * @time: 15:45
 */
@Service
public class SlideshowImageServiceImpl implements SlideshowImageService {

    @Autowired
    SlideshowImageMapper slideshowImageMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /*轮播图全查*/
    @Override
    public CommonResult list(PageRequest pageRequest,SlideshowImage slideshowImage) {
        MyPageHelper.startPage(pageRequest);
        List<SlideshowImage> list = slideshowImageMapper.list(slideshowImage);
        return CommonResult.ok(MyPageHelper.getPageResult(list));
    }

    /*轮播图上传*/
    @Override
    public CommonResult create(MultipartFile file, SlideshowImage slideshowImage) throws IOException {
        slideshowImage.setCreateTime(new Date());
        slideshowImage.setStatus(0);
        int i = slideshowImageMapper.insertSelective(slideshowImage);
        //如果文件不为空时候发送给MQ
        if (file!=null){
            //MQ传输流固定byte类型
            byte[] bytes = file.getBytes();
            //Message构造器携带返回id为消息id方便插入数据
            Message message = MessageBuilder.withBody(bytes).setMessageId(String.valueOf(slideshowImage.getId())).build();
            //发送到队列
            rabbitTemplate.convertAndSend(UploadBean.ZJX_UPLOAD_FILE_NAME,message);
        }
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }

    /*轮播图删除*/
    @Override
    public CommonResult remove(Integer id) {
        int i = slideshowImageMapper.deleteByPrimaryKey(id);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }

    /*轮播图信息修改*/
    @Override
    public CommonResult update(MultipartFile file, SlideshowImage slideshowImage) throws IOException {
        //如果文件不为空时候发送给MQ
        if (file!=null){
            //MQ传输流固定byte类型
            byte[] bytes = file.getBytes();
            //Message构造器携带返回id为消息id方便插入数据
            Message message = MessageBuilder.withBody(bytes).setMessageId(String.valueOf(slideshowImage.getId())).build();
            //发送到队列
            rabbitTemplate.convertAndSend(UploadBean.ZJX_UPLOAD_FILE_NAME,message);
        }
        int i = slideshowImageMapper.updateByPrimaryKeySelective(slideshowImage);
        if (i!=0){
            return CommonResult.ok();
        }else{
            return CommonResult.fail(888,"网络异常");
        }
    }
}
